#include "StdAfx.h"
#include "D3DRenderer.h"

#include "Renderable.h"

using namespace RenderSystem;

D3DRenderer::D3DRenderer(void)
{
}


D3DRenderer::~D3DRenderer(void)
{
}

void D3DRenderer::DrawScene( void )
{	
}

bool D3DRenderer::_RenderDPGPrePass( RenderQueue* pRenderQueue )
{
	bool bDirty = FALSE;
	// Draw the static occluder primitives using a depth drawing policy.
	{
		// Draw opaque occluders which support a separate position-only vertex buffer to minimize vertex fetch bandwidth,
		// which is often the bottleneck during the depth only pass.
		SCOPED_DRAW_EVENT(EventPosOnly)(DEC_SCENE_ITEMS,TEXT("PosOnly Opaque"));
		bDirty |= Scene->DPGs[DPGIndex].PositionOnlyDepthDrawList.DrawVisible(View,View.StaticMeshOccluderMap);
	}
	{
		// Draw opaque occluders, using double speed z where supported.
		SCOPED_DRAW_EVENT(EventOpaque)(DEC_SCENE_ITEMS,TEXT("Opaque"));
		bDirty |= Scene->DPGs[DPGIndex].DepthDrawList.DrawVisible(View,View.StaticMeshOccluderMap);
	}
	// Only render masked materials if scene depth needs to be up to date after the prepass
	if (bRenderDominantLightShadowsForBasePass)
	{
		SCOPED_DRAW_EVENT(EventOpaque)(DEC_SCENE_ITEMS,TEXT("Masked"));
		bDirty |= Scene->DPGs[DPGIndex].MaskedDepthDrawList.DrawVisible(View,View.StaticMeshOccluderMap);
	}

	// Only render masked materials if scene depth needs to be up to date after the prepass
	const EDepthDrawingMode DepthDrawingMode = bRenderDominantLightShadowsForBasePass ? DDM_AllOccluders : DDM_NonMaskedOnly;
	// Draw the dynamic occluder primitives using a depth drawing policy.
	TDynamicPrimitiveDrawer<FDepthDrawingPolicyFactory> Drawer(&View,DPGIndex,DepthDrawingMode,TRUE);
	{
		SCOPED_DRAW_EVENT(EventDynamic)(DEC_SCENE_ITEMS,TEXT("Dynamic"));
		for(INT PrimitiveIndex = 0;PrimitiveIndex < View.VisibleDynamicPrimitives.Num();PrimitiveIndex++)
		{
			const FPrimitiveSceneInfo* PrimitiveSceneInfo = View.VisibleDynamicPrimitives(PrimitiveIndex);
			const FPrimitiveViewRelevance& PrimitiveViewRelevance = View.PrimitiveViewRelevanceMap(PrimitiveSceneInfo->Id);

			const FLOAT LODFactorDistanceSquared = (PrimitiveSceneInfo->Bounds.Origin - View.ViewOrigin).SizeSquared() * Square(View.LODDistanceFactor);
			if( PrimitiveSceneInfo->bUseAsOccluder &&
				PrimitiveViewRelevance.GetDPG(DPGIndex) && 
				// Render the primitive if scene depth needs to be up to date after the prepass,
				// Or the primitive takes up enough screen space to be a good occluder.
				(bRenderDominantLightShadowsForBasePass || Square(PrimitiveSceneInfo->Bounds.SphereRadius) > MinScreenRadiusForDepthPrepassSquared * LODFactorDistanceSquared) &&
				// Only draw opaque primitives
				PrimitiveViewRelevance.bOpaqueRelevance
				)
			{
				Drawer.SetPrimitive(PrimitiveSceneInfo);
				PrimitiveSceneInfo->Proxy->DrawDynamicElements(
					&Drawer,
					&View,
					DPGIndex
					);
			}
		}
	}
	bDirty |= Drawer.IsDirty();
	return bDirty;
}
